package org.dicker.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.dicker.dao.ReplyDao;
import org.dicker.entity.MessageInfo;
import org.dicker.entity.ReplyInfo;
import org.dicker.entity.UserInfo;
import org.dicker.util.ORMUtil;

public class ReplyDaoImpl implements ReplyDao{

	@Override
	public List<ReplyInfo> selectByList(int page, int pageSize) {
		String sql="SELECT t1.reply_id,t1.content,date_format(t1.creat_time, '%Y-%m-%d %H:%I:%S') creat_time"
				+ ",t2.message_title,t3.user_name,t3.user_img from tb_reply t1 inner join tb_message t2 "
				+ "on t1.message_id=t2.message_id  inner join tb_user t3 "
				+ "on t1.user_id=t3.user_id";
		List<Object> valueList = new ArrayList<Object>();
		
		sql+=" ORDER BY creat_time desc";
		
		if(page!=-1 && pageSize!=-1){
			sql+=" LIMIT ?,?";
			valueList.add((page-1)*pageSize);
			valueList.add(pageSize);
		}
		return ORMUtil.getInstance().selectList(ReplyInfo.class, sql, valueList.toArray());
	}

	@Override
	public int selectCount(int message_id) {
		String sql="SELECT count(*) from tb_reply where 1=1";
		
		List<Object> valueList = new ArrayList<Object>();
		if(message_id!=-1){
			sql+=" and message_id=?";
			valueList.add(message_id);
		}
		
		long value= (long) ORMUtil.getInstance().selectSingleValue("count(*)", sql,valueList.toArray());
		return (int) value;
	}

	@Override
	public boolean addReply(ReplyInfo rInfo) {
		String sql="INSERT INTO tb_reply(message_id,user_id,content,creat_time) VALUES (?,?,?,NOW())";
		return ORMUtil.getInstance().executeDML(sql,rInfo.getMessage_id(),rInfo.getUser_id(),
				rInfo.getContent())>0;
	}

	@Override
	public boolean selectUserid(int user_id) {
		String sql="SELECT * from tb_user where user_id=?";
		List<UserInfo> ulist = ORMUtil.getInstance().selectList(UserInfo.class, sql, user_id);
		
		return ulist.size()>0? true:false;
	}

	@Override
	public int deleteById(int replyid) {
		String sql="DELETE FROM tb_reply WHERE reply_id=?";
		return ORMUtil.getInstance().executeDML(sql, replyid);
	}

	@Override
	public ReplyInfo selectByInfo(int id) {
		String sql="SELECT * from tb_reply where reply_id=?";
		List<ReplyInfo> rList=ORMUtil.getInstance().selectList(ReplyInfo.class, sql, id);
		return rList.size()>0? rList.get(0):null;
	}

	@Override
	public boolean updateById(ReplyInfo rinfo) {
		String sql="update tb_reply set content=? where reply_id=?";
		return ORMUtil.getInstance().executeDML(sql, rinfo.getContent(),rinfo.getReply_id())>0;
	}

	@Override
	public List<ReplyInfo> selectByMessageId(int message_id) {
		String sql="SELECT t1.reply_id,t1.content,date_format(t1.creat_time, '%Y-%m-%d %H:%I:%S') creat_time"
				+ ",t2.message_title,t3.user_name,t3.wx_openid,t3.user_img from tb_reply t1 inner join tb_message t2 "
				+ "on t1.message_id=t2.message_id  inner join tb_user t3 "
				+ "on t1.user_id=t3.user_id where t1.message_id=? ORDER BY creat_time desc ";
		return ORMUtil.getInstance().selectList(ReplyInfo.class, sql, message_id);
	}

	@Override
	public List<ReplyInfo> selectByUser(String wx_openid) {
		String sql="SELECT t1.reply_id,t1.content,date_format(t1.creat_time, '%Y-%m-%d %H:%I:%S') creat_time"
				+ ",t2.message_title,t3.user_name,t3.wx_openid,t3.user_img from tb_reply t1 inner join tb_message t2 "
				+ "on t1.message_id=t2.message_id  inner join tb_user t3 "
				+ "on t1.user_id=t3.user_id where t3.wx_openid=?";
		return ORMUtil.getInstance().selectList(ReplyInfo.class, sql, wx_openid);
	}

	

}
